package net.cathackers.devel.scmp.xmpp;

import java.util.Iterator;
import java.util.LinkedList;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Namespace;
import org.dom4j.QName;

import net.cathackers.devel.scmp.auth.impl.jabber.JabberUserID;
import net.cathackers.devel.scmp.auth.interfaces.UserID;
import net.cathackers.devel.scmp.chat.impl.xmpp.iqcomponent.roster.RosterItem;
import net.cathackers.devel.scmp.chat.interfaces.RosterSubscriptionManager.Ask;
import net.cathackers.devel.scmp.chat.interfaces.RosterSubscriptionManager.Subscription;

public class RosterPacket extends IQPacket {

    public RosterPacket(IQType type) {
	super(type);
           rootElement_.addElement("query", "jabber:iq:roster");
    }

    public RosterPacket(Document doc) {
	super(doc);
    }
    
    public RosterPacket(Packet packet) {
	super(packet);
    }
    
    public RosterPacket() {
	super();
    }

    public void addRosterItem(RosterItem rosterItem) {
	Element query_ = rootElement_.element(new QName("query", Namespace.get("jabber:iq:roster")));
	Element item = query_.addElement("item");
	item.addAttribute("jid", rosterItem.getJID().getCanonicalName());
	item.addAttribute("name", rosterItem.getNickname());
	item.addAttribute("subscription", rosterItem.getSubscription().getStringValue());
	item.addAttribute("ask", rosterItem.getAsk().getStringValue());
	Element group = item.addElement("group");
	if (rosterItem.getGroup() != null) {
	    group.setText(rosterItem.getGroup()); 
	}
	

    }

    public void removeRosterItem(UserID jid) {
	
	Element query_ = rootElement_.element(new QName("query", Namespace.get("jabber:iq:roster")));
	for (Iterator<Element> i = query_.elementIterator(); i.hasNext(); ) {
            Element item = i.next();
            if ((item.attributeValue("jid")).equals(jid.getGeneralUserID())) {
                query_.remove(item);
            }
        }

    }

    public RosterItem updateRosterItem(RosterItem item) {
	RosterItem rosterItem = null ;
	Element query_ = rootElement_.element("query");
	   for (Iterator<Element> i = query_.elementIterator(); i.hasNext(); ) {
	            Element item1 = i.next();
	            if ((item1.attributeValue("jid")).equals(item.getJID().getGeneralUserID())) {
	        	 item1.attributeValue("name", item.getNickname());
	        	 item1.attributeValue("subscription", item.getSubscription().toString());
	        	 item1.attributeValue("ask", item.getAsk().getStringValue());
	        	 item1.element("qroup").setText(item.getGroup());
	            }
	            rosterItem = this.getRosterItem(item1);
	   }
	return rosterItem;

    }

    public LinkedList<Element> getAllItems() {
	LinkedList<Element> ret = new LinkedList<Element>();
	Element query_ = rootElement_.element(new QName("query", Namespace.get("jabber:iq:roster")));
	   for (Iterator<Element> i = query_.elementIterator(); i.hasNext(); ) {
	       Element item = i.next();
	    ret.add(item);
	   }
	
	return ret;
    }

    public RosterItem getRosterItem(Element item) {
	UserID jid = new JabberUserID(item.attributeValue("jid"));
	String nickname = item.attributeValue("name");
	String subscriptionString = item.attributeValue("subscription");
	String askString = item.attributeValue("ask");
	Ask ask = RosterItem.getAskType(askString);
	Subscription subscription = RosterItem.getSubType(subscriptionString);
	String group = item.elementText("group");
	return new RosterItem(jid, nickname, ask, subscription, group);
    }

}
